package otw.utils.pagination;

import java.util.List;
import java.util.Map;

/**
 * 组装一页数据的模型类
 * 
 * @author space
 * @date Sep 29, 2008 3:13:06 PM
 */
public class Page<T> {
	/** 页面大小 */
	private Integer size;
	/** 起始记录偏移 */
	private Integer start;
	/** 总记录数 */
	private Integer total;
	/** 当前页码 */
	private Integer pageNum;
	/** 总页数 */
	private Integer totalPageNum;
	/** 一页数据 */
	private List<T> data;
	private Map<String, Object> paramMap;

	/** size 参数的名字 */
	public static final String SIZE_PARAM = "size";
	/** start 参数的名字 */
	public static final String START_PARAM = "start";
	/** pageNum 参数的名字 */
	public static final String PAGE_NUM_PARAM = "pageNum";

	/** 默认 size */
	public static final Integer SIZE_DEFAULT = 20;
	/** size 限制 */
	public static final Integer SIZE_MAX_LIMIT = 100;

	/**
	 * 获得一页中的实际记录数；
	 * 
	 * @return
	 */
	public Integer getRealSize() {
		if (data == null)
			return Integer.valueOf(0);
		else
			return Integer.valueOf(data.size());
	}

	/**
	 * 计算页码
	 * 
	 * @param pageNum
	 * @param size
	 * @param totalCount
	 * @return
	 */
	public static Integer figurePageNum(Integer pageNum, Integer size, Integer totalCount) {
		if (totalCount == 0 || pageNum < 1 || size < 1)
			return 0;
		Integer maxPageNum = totalCount / size;
		if (totalCount % size > 0)
			maxPageNum = maxPageNum + 1;
		return pageNum <= maxPageNum ? pageNum : maxPageNum;
	}

	/**
	 * 修正页码
	 * @param pageNum
	 * @param maxPageNum
	 * @return
	 */
	public static Integer figurePageNum(Integer pageNum, Integer maxPageNum) {
		if (pageNum < 1 || maxPageNum < 1)
			return 0;
		return pageNum <= maxPageNum ? pageNum : maxPageNum;
	}

	/**
	 * 计算总页数
	 * 
	 * @param size
	 * @param totalCount
	 * @return
	 */
	public static Integer figureMaxPageNum(Integer size, Integer totalCount) {
		if (totalCount == 0 || size < 1)
			return 0;
		Integer maxPageNum = totalCount / size;
		if (totalCount % size > 0)
			maxPageNum = maxPageNum + 1;
		return maxPageNum;
	}

	/**
	 * 计算起始记录偏移
	 * 
	 * @param pageNum
	 * @param size
	 * @param totalCount
	 * @return
	 */
	public static Integer figureStart(Integer pageNum, Integer size, Integer totalCount) {
		if (totalCount == 0 || pageNum < 1 || size < 1)
			return 0;
		Integer start = (pageNum - 1) * size;
		// 如果超出了总数范围，则重新计算
		if (start >= totalCount) {
			Integer remainder = totalCount % size;
			if (remainder == 0) {
				start = totalCount - size;
			} else {
				start = totalCount - remainder;
			}
		}
		return start;
	}

	public static void main(String args[]) {
		// 测试 figurePageNum
		System.out.println(figurePageNum(3, 10, 60) == 3 ? "通过1" : "失败1");
		System.out.println(figurePageNum(20, 10, 60) == 6 ? "通过2" : "失败2");
		System.out.println(figurePageNum(20, 10, 61) == 7 ? "通过3" : "失败3");

		System.out.println("---------");

		// 测试 figureStart
		System.out.println(figureStart(2, 7, 30) == 7 ? "通过1" : "失败1");
		System.out.println(figureStart(2, 7, 7) == 0 ? "通过2" : "失败2");
		System.out.println(figureStart(2, 7, 6) == 0 ? "通过3" : "失败3");
		System.out.println(figureStart(2, 7, 12) == 7 ? "通过4" : "失败4");
		System.out.println(figureStart(2, 7, 0) == 0 ? "通过5" : "失败5");
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getTotalPageNum() {
		return totalPageNum;
	}

	public void setTotalPageNum(Integer totalPageNum) {
		this.totalPageNum = totalPageNum;
	}

	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}

	public Map<String, Object> getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map<String, Object> paramMap) {
		this.paramMap = paramMap;
	}
}